package cn.softgame.jserver.application.connection;

import cn.softgame.jserver.application.Connection;
import cn.softgame.jserver.application.connection.ServerCfg;
import cn.softgame.jserver.application.connection.TcpConnection;
import cn.softgame.jserver.application.connection.UdpConnection;
import cn.softgame.jserver.connector.tcp.TCPClient;
import cn.softgame.jserver.connector.tcp.TCPReactor;
import cn.softgame.jserver.connector.udp.UDPClient;
import cn.softgame.jserver.connector.udp.UDPReactor;

/**
 * 连接工厂，用于获得Connection连接
 * @author xuquan
 * 日期:2013-5-28
 */
public class ConnectionFactory {
	private TCPClient cclient=null;
	private UDPClient uclient=null;
	private TCPReactor tserver=null;
	private UDPReactor userver=null;
	
	private int fType; //是服务器端的还是CLient端
	public final static int CLIENT=1;
	public final static int TCPSERVER=2;
	public final static int UDPSERVER=3;
	
	private static ConnectionFactory factory;
	public synchronized static ConnectionFactory getInstance(int fType){
		if(factory==null){
			factory=new ConnectionFactory(fType);
		}
		return factory;
	}
	private ConnectionFactory(int fType){
		this.fType=fType;
	}
	
	/**
	 * 获得一个TcpConnection实例
	 * @return Connection
	 */
	public Connection getTcpConnection(boolean longConnection){
		if(fType==CLIENT){
			startTcpClientSocket(longConnection);
			return new TcpConnection(cclient);
		}else{
			startTcpServerSocket();
			return new TcpConnection();
		}
	}
	
	public Connection getUdpConnection(){
		if(fType==CLIENT){
			startUdpClientSocket();
			return new UdpConnection(uclient);
		}else{
			startUdpServerSocket();
			return new UdpConnection();
		}
	}
	
	/**
	 * 开启客户端Tcp Socket线程
	 * @param longConnection
	 */
	private void startTcpClientSocket(boolean longConnection){
		if(cclient!=null){
			return;
		}
		try{
			cclient = new TCPClient(longConnection);
			Thread thread = new Thread(cclient);
			thread.setDaemon(true);
			thread.start();
			System.out.println("started Tcp Socket thread ..");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 开启客户端Udp Socket线程
	 */
	private void startUdpClientSocket(){
		if(uclient!=null){
			return;
		}
		try{
			uclient = new UDPClient();
			Thread thread=new Thread(uclient);
			thread.setDaemon(true);
			thread.start();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	   * 开启服务器端TCP Socket线程
	   */
	  private void startTcpServerSocket() {
	    if (tserver != null)
	      return;
	    try {
	      ServerCfg cfg = new ServerCfg();
	      tserver = new TCPReactor(cfg.getTcpPort());
	      Thread thread = new Thread(tserver);
	      thread.setDaemon(true);
	      thread.start();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	  }

	  /**
	   * 开启服务器端UDP Socket线程
	   */
	  private void startUdpServerSocket() {
	    if (userver != null)
	      return;
	    try {
	      ServerCfg cfg = new ServerCfg();
	      userver = new UDPReactor(cfg.getUdpPort());
	      Thread thread = new Thread(userver);
	      thread.setDaemon(true);
	      thread.start();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	  }
}
